package com.wanpinghui.wphlibrary.webapp;

import android.text.TextUtils;
import android.webkit.JavascriptInterface;

import com.google.gson.Gson;
import com.google.gson.internal.LinkedTreeMap;
import com.google.gson.reflect.TypeToken;
import com.wanpinghui.wphlibrary.WPHLibrary;
import com.wanpinghui.wphlibrary.common.UrlConstants;
import com.wanpinghui.wphlibrary.model.product.bean.ProductBean;
import com.yunshl.yunshllibrary.utils.DevicesUtil;
import com.yunshl.yunshllibrary.utils.TextUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.concurrent.TimeUnit;

import retrofit.ToKenUtil;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;

/***
 * 功能描述:JS调用app的方法
 * 作者:qiujialiu
 * 时间:2017/6/20
 ***/

public class JSMethod {
    private String baseUrl;
    private OnJsInvokeListener listener;
    private String extra;

    @JavascriptInterface
    public String loadToken() {
        return ToKenUtil.getToken();
    }

    @JavascriptInterface
    public String loadAPI() {
        return UrlConstants.URL + "api/v2";
    }

    public JSMethod(String baseUrl, OnJsInvokeListener listener) {
        this.baseUrl = baseUrl;
        this.listener = listener;
    }

    public JSMethod(String baseUrl, String extra ,OnJsInvokeListener listener) {
        this.baseUrl = baseUrl;
        this.listener = listener;
        this.extra = extra;
    }

    @JavascriptInterface
    public String loadVersion() {
        return DevicesUtil.getVersion(WPHLibrary.getLibrary().getContext());
    }

    /**
     * JSCallOpTypeJump,      //跳新页面0
     * JSCallOpTypeGoBack,     //返回1
     * JSCallOpTypeTokenInValue,   //TOKEN无效2
     * JSCallOpTypePickPhoto,     //选择图片3（需要传已选图片个数，{pick_num:x}）
     * JSCallOpTypeUploadPhotos,  //图片上传4（需要传图片的数组json【Base64的数组json】）
     *JSCallOpTypeUpdatePro,  //更新app的产品5 (需要传产品的json，{p_json:x})
     JSCallOpTypeGoCart,  //去购物车6
     JSCallOpTypeSelectFormat, //选择规格7
     * @param params
     */
    @JavascriptInterface
    public void jsCallAppParam(final String params) {
        Observable.timer(20, TimeUnit.MILLISECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<Long>() {
                    @Override
                    public void call(Long aLong) {
                        WPHWebView.WebParams webParams = new Gson().fromJson(params, new TypeToken<WPHWebView.WebParams>(){}.getType());
                        if (webParams.type == 1) {
                            if (listener != null) {
                                listener.goBack();
                            }
                        }else if (webParams.type == 2) {
                            if (listener != null) {
                                listener.tokenInvalid();
                            }
                        }else if (webParams.type == 3) {
                            if (listener != null) {
                                listener.onSelectPhoto(webParams.param.pics);
                            }
                        }else if (webParams.type == 4) {
                            if (listener != null) {
                                listener.onUploadPhoto(webParams.param.pics);
                            }
                        }else if (webParams.type == 5) {
                            if (webParams.param.p_json != null) {
                                if (listener != null) {
                                    listener.onLoadProduct(webParams.param.p_json);
                                }
                            }
                        }else if (webParams.type == 6) {
                            if (listener != null) {
                                listener.goShoppingCart();
                            }
                        }else if (webParams.type == 7) {
                            if (listener != null) {
                                listener.onSelectFormat(null);
                            }
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        throwable.printStackTrace();
                    }
                });
    }

}
